﻿using System;
using System.Collections.Generic;
using System.Linq;
using Quest.PowerGUI;
using Quest.PowerGUI.ScriptEditor;
using System.Reflection;
using Quest.PowerGUI.ScriptEditor.Shared;
using Quest.PowerGUI.Engine.Shell;
using Quest.PowerGUI.Engine.Shell.Host;
using System.Management.Automation.Runspaces;
using System.Diagnostics;

namespace PowerGuiVsx.Core
{
    public class ConfigLoader
    {
        private static SettingsManager _settingsManager;
        private static readonly object Locker = new object();
        private static MonadShell _shell;

        public static void Callback(HostCallbackParameter param)
        {
        }

        public static void LoadSettings()
        {
            try
            {

                Logger s_CurrentLogger = null;
                string path = System.IO.Path.Combine(ProductFolder.UserDataFolder, ProductFolder.SpecialFile.EditorConfig);
                string strAlternatePath = System.IO.Path.Combine(ProductFolder.UserDataFolder, ProductFolder.SpecialFile.OldEditorConfig);
                if (!XMLSettingsManager.FileExists(path, strAlternatePath))
                {
                    XMLSettingsManager.CreateEmptyConfigFile(path, false);
                }
                using (SettingsManager manager = new XMLSettingsManager())
                {
                    using (SettingsManager manager2 = new XMLSettingsManager(path))
                    {
                        SettingsManager.Current = manager2;

                        lock (Locker)
                        {
                            try
                            {
                                manager2.Load();
                            }
                            catch
                            {
                                XMLSettingsManager.CreateEmptyConfigFile(path, true);
                                manager2.Load();
                            }
                            typeof(EditorSettings).InvokeMember("Load", BindingFlags.NonPublic | BindingFlags.InvokeMethod | BindingFlags.Static, null, null, new[] { manager2 });

                            manager2.DialogSettings.LogOptions.LogLevel = EditorSettings.LogLevel;
                            manager2.DialogSettings.LogOptions.EraseLogOnExit = EditorSettings.EraseLogOnExit;

                            s_CurrentLogger = new Logger(ProductFolder.SpecialFile.EditorLog);
                            s_CurrentLogger.SourceLevel = EditorSettings.LogLevel;
                            s_CurrentLogger.EraseLogOnExit = EditorSettings.EraseLogOnExit;
                            manager2.Logger = s_CurrentLogger;
                            string logPath = s_CurrentLogger.LogPath;
                            PowerGUISettings.InitSettings(manager);
                            SettingsManager.ConsoleSettings = new ConsoleSettings(manager);
                            SettingsManager.ConsoleSettings.Settings.Logger = manager2.Logger;
                            try
                            {
                                PrepareConfigFolder.PrepareConfig();
                            }
                            catch
                            {
                            }

                            _settingsManager = manager2;
                        }
                    }
                }
            }
            catch (Exception exception2)
            {
                throw exception2;
            }
            finally
            {
            }


            _shell = new MonadShell(Callback);
#if notyet
            try
            {
                _shell.SetApartmentStateAndOpen(_settingsManager.STAMode);
                var runspace = _shell.Runspace as Runspace;
                var pipeline = runspace.CreatePipeline();

                var command = new Command("New-PSSession");
                command.Parameters.Add(new CommandParameter("Name", "visualStudioInternalDebugSession"));

                var command2 = new Command("Enter-PSSession");
                command2.Parameters.Add(new CommandParameter("Name", "visualStudioInternalDebugSession"));

                pipeline.Commands.Add(command);
                pipeline.Commands.Add(command2);

                pipeline.Invoke();

                pipeline.Dispose();
            }
            catch (Exception ex)
            {
                Trace.WriteLine("An exception was thrown while trying to initialize the debugging system. Debugging will not be available in this session");
            }
#endif
            ((IHostPart)_shell).Initialize(SettingsManager.ConsoleSettings.Settings);
        }
    }
}
